package snippets.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUtil {
	/**
	 * 方法描述:getConnection
	 * @作者:hjl
	 * @日期:2012-10-12 时间:下午10:31:35
	 * @功能:得到Connection 
	 * @param url
	 * @param user
	 * @param password
	 * @return
	 */
	public Connection getConnection(String className,String url,String user,String password){
		Connection connection = null;
		try {
			Class.forName(className);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			connection = DriverManager.getConnection(url,user,password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
	/**
	 * 方法描述:getStatement
	 * @作者:hjl
	 * @日期:2012-10-13 时间:上午09:50:55
	 * @功能:得到Statement 
	 * @param connection
	 * @return
	 */
	public synchronized Statement getStatement(Connection connection){
		Statement statement = null;
		try {
			statement = connection.createStatement();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return statement;
	} 	
	/**
	 * 方法描述:update
	 * @作者:hjl
	 * @日期:2012-10-12 时间:下午10:06:38
	 * @功能:执行新建,删除,插入,更新操作 
	 * @param connection
	 * @param expression
	 */
	public synchronized void update(Connection connection ,String expression){
        Statement statement = null;
        int i=0;
        try {
			statement = connection.createStatement();
			i = statement.executeUpdate(expression);
			closeStatement(statement);
		} catch (SQLException e) {
			e.printStackTrace();
		}
        if (i == -1) {
            out("db error : " + expression);
        }
	}
	/**
	 * 方法描述:query
	 * @作者:hjl
	 * @日期:2012-10-12 时间:下午10:13:46
	 * @功能:执行查询操作 
	 * @param statement
	 * @param expression
	 * @return
	 * @throws SQLException
	 */
	public synchronized ResultSet query(Statement statement,String expression){
		ResultSet resultSet = null;
		try {
			resultSet = statement.executeQuery(expression);
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		return resultSet;
	}
	/**
	 * 方法描述:dump
	 * @作者:hjl
	 * @日期:2012-10-12 时间:下午10:28:21
	 * @功能:打印出查询结果 
	 * @param rs
	 * @throws SQLException
	 */
    public void dump(ResultSet rs){
        ResultSetMetaData resultSetMetaData = null;
        int colmax = 0;
        int i = 0;
        Object object = null;
		try {
			resultSetMetaData = rs.getMetaData();
			colmax = resultSetMetaData.getColumnCount();
			for (; rs.next(); ) {
			    for (i = 0; i < colmax; ++i) {
			        object = rs.getObject(i + 1);
			        System.out.print(object.toString()+" ");
			    }
			    out("\n");
			}			
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
    }
	/**
	 * 方法描述:closeConnection
	 * @作者:hjl
	 * @日期:2012-10-12 时间:下午10:23:13
	 * @功能:关闭Connection 
	 * @param connection
	 */
	public void closeConnection(Connection connection){
		try {
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}    
	/**
	 * 方法描述:closeStatement
	 * @作者:hjl
	 * @日期:2012-10-12 时间:下午10:22:13
	 * @功能:关闭 Statement
	 * @param statement
	 */
	public void closeStatement(Statement statement){
		try {
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 方法描述:closeResultSet
	 * @作者:hjl
	 * @日期:2012-10-12 时间:下午10:22:37
	 * @功能:关闭ResultSet
	 * @param resultSet
	 */
	public void closeResultSet(ResultSet resultSet){
		try {
			resultSet.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	private void out(Object object){
		System.out.println(object);
	}	
}
